
#include "syscall.h"

int main(int argc, char** argv)
{
   int ret = 0;
   char* filename = "hello.txt";
   char* toWrite = "hello world";
   char* prog = "open2.coff";

   //create file
   int fd = 0;
   if( ret == 0 ) {
      fd = creat( filename );
      if( fd != -1 ) {
         printf( "FileDescriptor for %s is %d\n", filename, fd );
      } else {
         printf( "error: could not create file: %s\n", filename );
         ret = -1;
      }
   }

    //create a child process that opens the original file and calls unlink
    int pid1 = 0;
    char* argv1[0];
    pid1 = exec(prog, 1, argv1);
    if (pid1 == -1) {
        printf("exec failed.\n");
        return;
    }

    //have the parent process join the child process
    int status1 = 0;
    join(pid1, &status1);

   //close fd from creat 
   //       The file should close and be deleted at this point
   int closeRetVal = 0;
   if( ret == 0 ) {
      closeRetVal = close( fd );
      if( closeRetVal == 0 ) {
         printf( "FileDescriptor %d closed successfully\n", fd );
      } else {
         printf( "error: could not close file: %s with fd: %d\n", 
                 filename, fd );
         ret = -5;
      }
   }

   return ret;
}
